package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.RiskModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 *  投核保校验   HMT051
 * @author :  zlc
 * @date : 2019/9/25 11:45
 **/
public class Hmt051InsuranceStrategy implements InsuranceStrategy{
    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, ProductConfig productConfig) {
       
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        //投保人职业类别
        if (StringUtils.isEmpty(custInfoModel.getAppnt().getOccupationType())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0026_MESSAGE);
        }
        //被保人信息
        PolicyInfoModel policyInfoModel = requestModel.getPolicyInfo();
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
       
        String prtNo = policyInfoModel.getPrtNo();
       
        //被保人序号
        int prodInsSeq = productModel.getProdInsSeq();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                String type = insurantModel.getOccupationType();
                //与投保人的关系
                String relation = insurantModel.getRelToApp();
                //2.校验投保人和被保人的关系
                if (!(productConfig.getRelationList().contains(relation))) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
                }
                //3.非HCT011的险种，职业类别不能为5、6、7类
                if (productConfig.getInsurantTypeList().contains(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE,type));
                }
                //1.校验职业类别是否填写 
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
               
                // 黑名单校验
                if (insurantModel.getBlackListFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE,insurantModel.getName()));
                }
                // 理赔标识
                if (insurantModel.getClaimFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0027,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0027_MESSAGE,insurantModel.getName()));
                }
                
            }
        }
        
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                List<RiskModel> result = insuredCoreRepository.getMillionHealthInsuranceCount(prtNo, insurantModel.getInsuredNo());
                for (RiskModel riskModel : result) {
                    // 百万的都用这个1  ，渠道那个用10 ，参考HMT049
                    if (StringUtils.equals("1", riskModel.getFlag()) ) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0032_MESSAGE);
                    }
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
    
}
